<script setup lang="ts">
import { useAuthStore } from '@/store/auth';
import { ref } from 'vue';
import RegisterPopup from './register.vue';
import img from "../../../assets/icon.png";
import { useRoute } from 'vue-router';

const route = useRoute();
const data = ref({
    username: '',
    password: ''
});

const showRegister = ref(false);

const onSubmit = async (values: { username: string, password: string}) => {
    const useStore = useAuthStore();
    try {
        const redirectPath = route.query.redirect as string || '/home';
        useStore.login(values.username, values.password, redirectPath);
    } catch (error) {
        console.error('登录失败', error);
    }
};

const openRegister = () => {
    showRegister.value = true;
};

const handleRegisterSuccess = () => {
    showRegister.value = false;
};
</script>

<template>
    <div class="container">
        <div class="login-card">
            <div class="image">
                <VanImage :src="img" width="100" height="100"></VanImage>
            </div>
            <div class="text">
                <h3>账号密码登录</h3>
            </div>
            <van-form class="form" @submit="onSubmit">
                <van-cell-group inset>
                    <van-field
                        v-model="data.username"
                        name="username"
                        label="用户名"
                        placeholder="用户名"
                        :rules="[{ required: true, message: '请填写用户名' }]"
                    />
                    <van-field
                        v-model="data.password"
                        type="password"
                        name="password"
                        label="密码"
                        placeholder="密码"
                        :rules="[{ required: true, message: '请填写密码' }]"
                    />
                </van-cell-group>
                <div class="submit-btn">
                    <van-button round block type="primary" native-type="submit">
                    登录
                    </van-button>
                </div>
            </van-form>
            <div class="register">
                <a @click="openRegister">注册账号</a>
            </div>
        </div>
        
        <RegisterPopup
            v-model:show="showRegister"
            @register-success="handleRegisterSuccess"
        />
    </div>
</template>

<style lang="scss" scoped>
.container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    max-width: 100%;
    background-image: url('http://web.yunnet.top:12900/witdata/background/login.png');
    background-size: cover;
    background-position: center;
    
    .login-card {
        display: flex;
        flex-direction: column;
        align-items: center;
        background-color: rgba(255, 255, 255, 0.85);
        border-radius: 16px;
        padding: 30px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        width: 90%;
        max-width: 420px;
        backdrop-filter: blur(8px);
    }

    .text {
        width: 100%;
        text-align: center;
        margin: 20px 0;
        h3 {
            font-size: 24px;
            color: #333;
            font-weight: 600;
        }
    }

    .image {
        margin-top: 10px;
        display: flex;
        justify-content: center;
    }

    .form {
        width: 100%;
    }

    .submit-btn {
        margin: 24px 16px 16px;
    }

    .register {
        width: 100%;
        text-align: right;
        margin-top: 10px;
        padding-right: 16px;
        
        a {
            color: #1989fa;
            text-decoration: none;
            cursor: pointer;
            &:hover {
                text-decoration: underline;
            }
        }
    }
}

@media (max-width: 480px) {
    .container {
        max-width: 100%;

        .login-card {
            padding: 10px;
        }
        
        .text {
            margin: 0;
            h3 {
                font-size: 20px;
            }
        }

        .image {
            margin-top: 10px;
            .van-image {
                width: 80px !important;
                height: 80px !important;
            }
        }
        
        .submit-btn {
            margin: 20px 10px 10px;
        }
    }
}
</style>
